package com.jkc.xh.fragments;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.jkc.xh.R;
import com.jkc.xh.adapters.RefferFriendAdapter;
import com.jkc.xh.adapters.core.BaseRecyclerAdapter;
import com.jkc.xh.constants.Constant;
import com.jkc.xh.fragments.core.BasePullRecyclerFragment;
import com.jkc.xh.manager.MoneyManager;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.RefferFriendResponse;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.utils.Tool;
import com.jkc.xh.views.widget.NavBar;
import com.jkc.xh.views.widget.ToastUtil;
import com.jkc.xh.views.widget.pullview.PullRecyclerView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * 推广明细
 */
@EFragment(R.layout.fragment_reffer_friend)
public class RefferFriendFragment extends BasePullRecyclerFragment implements BaseRecyclerAdapter.OnItemClickListener {

    @ViewById(R.id.nav_bar)
    NavBar nav_bar;
    @ViewById(R.id.tv_ref_cnt)
    TextView tv_ref_cnt;
    @ViewById(R.id.tv_ref_refund)
    TextView tv_ref_refund;

    private RefferFriendAdapter mAdapter;
    private int pageIndex = 0;
    private int pageTotal = 0;


    @Override
    protected void initView(PullRecyclerView recyclerView) {
        tv_ref_cnt.setText(getString(R.string.str_invite_cnt, "0"));
        tv_ref_refund.setText(getString(R.string.str_invite_refund, Tool.formatNumNoScale("0"), "0"));

        mAdapter = new RefferFriendAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(mAdapter);
        nav_bar.setOnMenuClickListener(new NavBar.OnMenuClickListener() {
            @Override
            public void onLeftMenuClick(View view) {
                finishFragment();
            }

            @Override
            public void onRightMenuClick(View view) {
                requestData(true);
            }
        });
        mAdapter.setItemClickListener(this);
    }


    @AfterViews
    void initData() {
        requestData(true);
    }

    private void requestData(final boolean isRefresh) {
        if (isRefresh) {
            pageIndex = 0;
            getPullRecyclerView().autoRefresh();
        } else {
            pageIndex++;
            if (pageIndex >= pageTotal) {
                return;
            }
        }
        addJob(MoneyManager.getRefferFriendList(pageIndex, new RestCallBack<RefferFriendResponse>() {
            @Override
            public void success(RefferFriendResponse response) {
                pageTotal = response.pages;
                tv_ref_cnt.setText(getString(R.string.str_invite_cnt, response.sum_friend));
                tv_ref_refund.setText(getString(R.string.str_invite_refund, Tool.formatNumNoScale(response.sum_bean), response.sum_score));
                if (response.list == null || response.list.size() == 0) {
                    finishLoad(false);
                    return;
                }
                if (isRefresh) {
                    mAdapter.replaceWith(response.list);
                } else {
                    mAdapter.addAll(response.list);
                }
                if (response.list.size() < Constant.PAGE_SIZE) {
                    finishLoad(false);
                } else {
                    finishLoad(true);
                }
            }

            @Override
            public void failure(RestError error) {
                ToastUtil.showToastShort(error.msg);
            }
        }));


    }

    @Override
    protected void onRefresh(RecyclerView recyclerView) {
        requestData(true);
    }

    @Override
    protected void onLoadMore(RecyclerView recyclerView) {
        requestData(false);
    }

    @Override
    public void onItemClick(View view, Object object, int position) {

    }
}
